local skel = fk.CreateSkill {
  name = "lb__shihong",
}

Fk:loadTranslationTable {
  ["lb__shihong"] = "释洪",
  [":lb__shihong"] = "每回合限一次，你可将一张黑色牌当无次数限制的雷【杀】使用，若此牌于手牌中满足：1.类型唯一；2.花色唯一；3.牌名字数唯一，你可展示手牌，令此牌最大目标数、抵消牌数、你本回合攻击范围改为达成项数。",

  ["#lb__shihong"] = "释洪：你可以将一张黑色牌当无次数限制的雷【杀】使用",
  ["#lb__shihong_ask"] = "释洪：你可以 %arg 额外指定至多 %arg2 个目标",
  ["#lb__shihong_show"] = "释洪：你可以展示所有手牌，令 %arg 最大目标数、抵消牌数、你本回合攻击范围改为 %arg2",

  ["@lb__shihong-turn"] = "释洪",

  ["$lb__shihong1"] = "成全你们。 ",
  ["$lb__shihong2"] = "都给我闭嘴！",
}


skel:addEffect("viewas", {
  anim_type = "offensive",
  pattern = "slash",
  prompt = "#lb__shihong",
  handly_pile = true,
  filter_pattern = {
    min_num = 1,
    max_num = 1,
    pattern = ".|.|black",
  },
  view_as = function(self, player, cards)
    if #cards ~= 1 then return end
    local c = Fk:cloneCard("thunder__slash")
    c.skillName = skel.name
    c:addSubcard(cards[1])
    return c
  end,
  before_use = function(self, player, use)
    local room = player.room
    local eid = use.card:getEffectiveId()
    local use_card = Fk:getCardById(eid)
    local cards = player:getCardIds("h")
    table.removeOne(cards, eid)
    local num = 3
    if table.find(cards, function(id)
          return Fk:getCardById(id).type == use_card.type
        end) then
      num = num - 1
    end
    if table.find(cards, function(id)
          return Fk:getCardById(id).suit == use_card.suit
        end) then
      num = num - 1
    end
    if table.find(cards, function(id)
          return Fk:translate(Fk:getCardById(id).trueName, "zh_CN"):len() ==
              Fk:translate(use_card.trueName, "zh_CN"):len()
        end) then
      num = num - 1
    end
    -- 获取使用牌的合法额外目标（为简化结算，不允许与已有目标重复、且【借刀杀人】等带副目标的卡牌使用首个目标的副目标）
    ---@param extra_data? UseExtraData | table
    ---@return ServerPlayer[]
    local function getExtraTargets(extra_data)
      if use.card.type == Card.TypeEquip or use.card.sub_type == Card.SubtypeDelayedTrick then return {} end
      local tos = {}
      --- 获取某个目标对应的副目标
      ---@param player ServerPlayer
      ---@return ServerPlayer[]
      local function getSubTos(player)
        use.subTos = use.subTos or {}
        for i, p in ipairs(use.tos) do
          if p == player then
            use.subTos[i] = use.subTos[i] or {}
            return use.subTos[i]
          end
        end
        return {}
      end
      local sub_tos = getSubTos(use.tos[1])
      for _, p in ipairs(RoomInstance.alive_players) do
        if not (table.contains(use.tos, p) or use.from:isProhibited(p, use.card)) and
            use.card.skill:modTargetFilter(use.from, p, {}, use.card, extra_data) then
          if #sub_tos > 0 then
            local mod_tos = { p }
            if table.every(sub_tos, function(sub_to)
                  if use.card.skill:modTargetFilter(use.from, sub_to, mod_tos, use.card, extra_data) then
                    table.insert(mod_tos, sub_to)
                    return true
                  end
                end) then
              table.insert(tos, p)
            end
          else
            table.insert(tos, p)
          end
        end
      end
      return tos
    end
    if num > 0 then
      if player.room:askToSkillInvoke(player, { skill_name = skel.name, prompt = "#lb__shihong_show:::" .. use.card:toLogString() .. ":" .. num }) then
        player:showCards(player:getCardIds("h"))
        room:setPlayerMark(player, "@lb__shihong-turn", num)
        use.extra_data = use.extra_data or {}
        use.extra_data.lb__shihong = num
        local tos = getExtraTargets()
        num = math.min(num - #use.tos, #tos)
        if num > 0 then
          local targets = room:askToChoosePlayers(player, {
            targets = tos,
            min_num = 1,
            max_num = num,
            skill_name = skel.name,
            cancelable = true,
            prompt = "#lb__shihong_ask:::" .. use.card:toLogString() .. ":" .. num,
          })
          if #targets > 0 then
            room:sendLog {
              type = "#AddTargetsBySkill",
              from = player.id,
              to = table.map(targets, Util.IdMapper),
              arg = skel.name,
              arg2 = use.card:toLogString(),
            }
            for _, p in ipairs(targets) do
              table.insert(use.tos, p)
            end
          end
        end
      end
    end
  end,
  enabled_at_play = function(self, player)
    return player:usedSkillTimes(skel.name) == 0
  end,
  enabled_at_response = function(self, player, response)
    return not response and player:usedSkillTimes(skel.name) == 0
  end,
})

skel:addEffect(fk.TargetConfirmed, {
  priority = 0.001,
  mute = true,
  is_delay_effect = true,
  can_trigger = function(self, event, target, player, data)
    return target == player and data.extra_data and data.extra_data.lb__shihong
  end,
  on_use = function(self, event, target, player, data)
    data:setResponseTimes(data.extra_data.lb__shihong, target)
  end,
})


skel:addEffect("targetmod", {
  bypass_times = function(self, player, skill, scope, card, to)
    return card and table.contains(card.skillNames, skel.name)
  end,
})

skel:addEffect("atkrange", {
  final_func = function (self, player)
    if player:getMark("@lb__shihong-turn") > 0 then
      return player:getMark("@lb__shihong-turn")
    end
  end,
})

return skel